package portal.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import dao.Query;
import portal.mybatis.dao.service.SockectLogService;
@Service
public class ScheduleSocketLogDao {
	@Autowired 
	SockectLogService sockectLogService;


    /**
     * 2018-7-12记录，algorithm_state的值为0，1，2，意义分别是：（4：失败，1：成功，2：运行中，3：暂停，5：终止）
     * @param logLevel
     * @param algorithmState
     * @param taskHash
     * @param subTaskHash
     * @param jobId
     * @return
     */
	@Deprecated
    public boolean insertJobState2(String logLevel, String algorithmState, String taskHash, String subTaskHash, String jobId){
        boolean result = false;
        result = insertLog(taskHash, subTaskHash,"",algorithmState,"",logLevel,"",jobId);
        return result;
    }
    public boolean  insertJobState(String logLevel, String algorithmState, String taskHash, String subTaskHash, String jobId){
    	return sockectLogService.insertLog(subTaskHash, subTaskHash, null, algorithmState, null, logLevel, null, jobId);
    }
    @Deprecated
    public boolean insertLog1(String taskHash, String subTaskHash, String logFileUri, String algorithmState,
                             String algorithmSchedule, String logLevel, String logMessage,String jobId){
        String sql = "insert into `socket_log`(task_hash, sub_task_hash, log_file_uri, algorithm_state, algorithm_schedule," +
                "log_level, time, log_message, job_id) values (?,?,?,?,?,?,now(),?,?)";
        Object[] params = new Object[]{taskHash,subTaskHash,logFileUri,algorithmState,algorithmSchedule,logLevel,logMessage,jobId};
        return Query.executeDML(sql, params)>0;
    }

    public boolean insertLog(String taskHash, String subTaskHash, String logFileUri, String algorithmState,
                             String algorithmSchedule, String logLevel, String logMessage,String jobId){
    	return sockectLogService.insertLog(taskHash, subTaskHash, logFileUri, algorithmState, algorithmSchedule, logLevel, logMessage, jobId);
    }
    @Deprecated
    public boolean updateLogFileByJobId1(String filePath, String jobId) {
        String sql = "update `socket_log` set log_file_uri = ?, time=now() where job_id = ?";
        Object[] params = new Object[]{filePath, jobId};
      
        return Query.executeDML(sql, params)>0;
    }
    public boolean updateLogFileByJobId(String logFileUri, String jobId) {
        return sockectLogService.updateByKeySelective(jobId, null, logFileUri,null);
    }

    /**
     * 2018-9-28记录，algorithm_state的值为0，1，2，意义分别是：（4：失败，1：成功，2：运行中，3：暂停，5：终止）
     * @param jobState
     * @param jobId
     * @return
     */
    @Deprecated
    public boolean updateJobStateByJobId1(String jobState, String jobId) {
        String sql = "";
        jobState = historyConvert2Log(jobState);
        Object[] params = new Object[]{jobState, jobId};
        if(jobState.equals("4")||jobState.equals("1")||jobState.equals("5"))
        {
            sql = "update `socket_log` set algorithm_state = ?, time=now() where job_id = ?";
        }
        else{
            sql = "update `socket_log` set algorithm_state = ?  where job_id = ?";
        }
        boolean result =  Query.executeDML(sql, params)>0;
        if(Integer.parseInt(jobState)==1)
        {
            result = result && updateJobScheduleByJobId("100",jobId);
        }
        return result;
    }
    
    public boolean updateJobStateByJobId(String jobState, String jobId) {
        String sql = "";
        jobState = historyConvert2Log(jobState);
        if(jobState.equals("4")||jobState.equals("5")){
        	return sockectLogService.updateByKeySelective(jobId, jobState, null,null);
        }else if(jobState.equals("1")){
        	return sockectLogService.updateByKeySelective(jobId, jobState, null,"100");
        }else{
        	return sockectLogService.updateByKeySelective(jobId, jobState, null,null,null);
          }
    }

    /**
     * 更新算法的进度信息
     * @param algorithmSchedule
     * @param jobId
     * @return
     */
    @Deprecated
    public boolean updateJobScheduleByJobId(String algorithmSchedule, String jobId) {
        String sql = "update `socket_log` set algorithm_schedule = ?, time=now() where job_id = ?";
        Object[] params = new Object[]{algorithmSchedule, jobId};
        return Query.executeDML(sql, params)>0;
    }
    public String historyConvert2Log(String jobStatus){
        String result = null;
        switch (jobStatus){
            case "DONE":
                result =  "1";
                break;
            case "EXIT":
                result =  "4";
                break;
            case "PAUSE":
                result =  "3";
                break;
            case "RUN":
            case "RUNNING":
                result =  "2";
                break;
            case "STOP":
                result =  "5";
                break;
            case "PEND":
            	 result="6";
            	 break;
            default:
                result = jobStatus;
        }
        return result;
    }
}
